import 'package:flutter/material.dart';
import 'package:flutter_project01/pages/home/index.dart';
import 'package:flutter_project01/pages/mine/index.dart';
import 'package:flutter_project01/utils/Emitter.dart';
import 'package:flutter_project01/utils/TokenManager.dart';

class TabsPage extends StatefulWidget {
  const TabsPage({super.key});

  @override
  _TabsPageState createState() => _TabsPageState();
}

class _TabsPageState extends State<TabsPage> {
  int _currentIndex = 0;
  @override
  void initState() {
    super.initState();
    // 初始化token
    tokenManager.init();
    // 注册监听事件
    registerEventBus();
  }

  registerEventBus() {
    // 监听 LogoutEvent事件
    eventBus.on<LogoutEvent>().listen((event) {
      //跳转登陆页
      Navigator.pushNamed(context, '/login');
    });
  }

  // 更新索引 父组件定义方法, 子组件调用
  updateIndex(int index) {
    setState(() {
      _currentIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // backgroundColor: const Color(0xffd5e3f2),
      // appBar: AppBar(
      //   backgroundColor: const Color(0xffd5e3f2),
      //   centerTitle: true,
      //   title: const Text('享+社区'),
      // ),
      body: IndexedStack(
        index: _currentIndex,
        children:  [
          HomePage(),
          MinePage(updateIndex: updateIndex),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        selectedItemColor: const Color(0xff5592af),
        unselectedItemColor: const Color(0xff434242),
        selectedFontSize: 16,
        unselectedFontSize: 16,
        currentIndex: _currentIndex,
        onTap: (int index) {
          setState(() {
            _currentIndex = index;
          });
        },
        items: [
          BottomNavigationBarItem(
              icon: Image.asset(
                'assets/tabs/home_default.png',
                width: 24,
                height: 24,
              ),
              activeIcon: Image.asset(
                'assets/tabs/home_active.png',
                width: 24,
                height: 24,
              ),
              label: '首页'),
          BottomNavigationBarItem(
              icon: Image.asset(
                'assets/tabs/my_default.png',
                width: 24,
                height: 24,
              ),
              activeIcon: Image.asset(
                'assets/tabs/my_active.png',
                width: 24,
                height: 24,
              ),
              label: '我的')
        ],
      ),
    );
  }
}
